(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:H5hdCtrl
     * @description
     * # H5hdCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('H5hdCtrl', H5hdCtrl);

    H5hdCtrl.$inject = ['$scope', 'dataService', 'helper', '$location', '$timeout', 'moment', '$rootScope'];

    function H5hdCtrl($scope, dataService, helper, $location, $timeout, moment, $rootScope) {

        $scope.listAction = function() {
            $scope.getList();
        }

        //获取列表
        $scope.getList = function(pageNo) {
            var query = {
                content: {
                    page_no: pageNo,
                    page_size: $scope.page_Size ? $scope.page_Size : 10,
                    store_id: $rootScope.store_id,
                }
            };

            dataService.h5hdGet(query).then(function(list) {
                if (list && list.h5hd_list) {
                    for (var i in list.h5hd_list) {
                        list.h5hd_list[i].diff_time = $scope.diff_time(list.h5hd_list[i].start_time, list.h5hd_list[i].end_time)
                    }
                    $scope.h5hd_list = list.h5hd_list;
                    $scope.total = list.total_count;
                }
            });
        }

        $scope.addAction = function() {
            
            $scope.h5hd_id = $location.search().h5hd_id;
            $scope.uid = random();
            $scope.uid1 = random();
            if ($scope.h5hd_id) {
                getOneData();
                $scope.$on('event:up-store-id', function() {
                    getOneData();
                });
            } else {
                $scope.h5hd = {
                    "store_id": $rootScope.store_id,
                    "title": "H5活动",
                    "start_time": moment().format('YYYY-MM-DD HH:mm:ss'),
                    "limitcount": 0,
                    "end_time": moment().add('days', 7).format('YYYY-MM-DD HH:mm:ss'),
                    "content1": "",
                    "content": "",
                };
                console.log(" $scope.h5hd", $scope.h5hd);
            }
        }

        function getOneData() {
            var query = {
                content: {
                    h5hd_id: $scope.h5hd_id,
                    store_id: $rootScope.store_id,
                }
            };

            // console.error("$rootScope", $rootScope);
            dataService.h5hdGet(query).then(function(result) {
                if (result.h5hd_list) {
                    $scope.h5hd = result.h5hd_list[0];
                    $scope.h5hd.limitcount = Number($scope.h5hd.limitcount);
                    console.error("$scope.h5hd", $scope.h5hd);
                    // $scope.h5hd.content = $scope.h5hd.content ? json2html(JSON.parse($scope.h5hd.content)) : "";
                }

            });
        }

        $scope.save = function() {
            var method = $scope.wxapp.appid ? "isvWxappUpdate" : "isvWxappAdd";
            dataService[method]($scope.wxapp).then(function(result) {
                console.log("------", result);
                if (result.Msg) {
                    helper.myAlert("错误：" + result.Msg);
                } else {
                    gourl("#/wxapp/list");
                }
            });
        }

        //活动添加
        $scope.h5hdAdd = function() {

            if ($scope.h5hd.end_time < $scope.h5hd.start_time) {
                helper.myAlert("活动结束时间不能小于活动开始时间");
                return false;
            }
            if (!$scope.h5hd.title) {
                helper.myAlert("活动标题不能为空");
                return false;
            }
            var data = {
                content: {
                    "store_id": $rootScope.store_id,
                    "gzh_appid": $scope.h5hd.gzh_appid,
                    "title": $scope.h5hd.title,
                    "start_time": $scope.h5hd.start_time,
                    "end_time": $scope.h5hd.end_time,
                    "img": $scope.h5hd.img,
                    "content1": $scope.h5hd.content1,
                    "limitcount": $scope.h5hd.limitcount,
                    "content": $scope.h5hd.content
                }
            }
            console.error("$scope.h5hd", data);
            // return;
            if ($scope.h5hd_id) {
                data.content.h5hd_id = $scope.h5hd_id;
                dataService.h5hdUpdate(data).then(function(result) {
                    if (result.h5hd_id) {
                        helper.myAlert("success", "#/h5hd/list");
                    } else {
                        helper.myAlert(result.Msg);
                    }
                });
            } else {
                dataService.h5hdAdd(data).then(function(result) {
                    if (result.h5hd_id) {
                        helper.myAlert("success", "#/h5hd/list");
                    } else {
                        helper.myAlert(result.Msg);
                    }
                });
            }
        }

        $scope.delete = function(appid) {
            dataService.isvWxappDelete(appid).then(function(result) {
                console.log("------", result);
                if (result.Msg) {
                    helper.myAlert("错误：" + result.Msg);
                } else {
                    $scope.getList();
                }
            });
        }

        //js计算两个时间的时间差
        $scope.diff_time = function(start_time, end_time) {
            var s_time = new Date().getTime();
            var start = new Date(start_time).getTime();
            var end = new Date(end_time).getTime();

            //当前时间是否大于活动结束时间
            if (s_time > end) {
                return 0;
            }

            //判断当前时间是否大于开始时间
            if (s_time > start && s_time < end) {
                var time = new Date().getTime();
            } else {
                var time = new Date(start_time).getTime();
            }

            //var diff = end_time * 1000 - start_time * 1000 //时间差的毫秒数  
            var diff = end - time;

            //计算出相差天数  
            var days = Math.floor(diff / (24 * 3600 * 1000));

            //计算出小时数  
            var leave1 = diff % (24 * 3600 * 1000); //计算天数后剩余的毫秒数  
            var hours = Math.floor(leave1 / (3600 * 1000));
            //计算相差分钟数  
            var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数  
            var minutes = Math.floor(leave2 / (60 * 1000));

            //计算相差秒数  
            var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数  
            var seconds = Math.round(leave3 / 1000);

            var returnStr = seconds + "秒";
            if (minutes > 0) {
                returnStr = minutes + "分" + returnStr;
            }
            if (hours > 0) {
                returnStr = hours + "小时" + returnStr;
            }
            if (days > 0) {
                returnStr = days + "天" + returnStr;
            }
            return returnStr;
        }
    }
})();